import * as api from '../../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    branch_mobile: '',
    repair_order: {},
    order_sn: '',
    comment: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const order_sn = options.order_sn
    this.setData({order_sn})
    this.orderDetail(order_sn)
  },

  orderDetail(order_sn) {
    const that = this
    api.customerRepairOrderDetail(order_sn)
      .then(res => {
        that.setData({
          ...res.data
        })
      }).catch(err => console.log(err))
  },

  callPhone(e) {
    const mobile = e.currentTarget.dataset.mobile
    if (mobile) {
      wx.makePhoneCall({
        phoneNumber: mobile
      })
    } else {
      wx.showToast({
        title: '未设置电话',
        icon: 'error'
      })
    }
  },

  inputChange(e) {
    const comment = e.detail.value
    this.setData({comment})
  },

  comment() {
    const that = this
    const order_sn = this.data.order_sn
    const comment = this.data.comment
    api.customerCommentRepairOrder(order_sn, comment)
      .then(res => {
        wx.showToast({
          title: '评论成功',
          icon: 'success'
        })
        that.orderDetail(order_sn)
      }).catch(err => console.log(err))
  },

  completeOrder() {
    const that = this
    const order_sn = that.data.order_sn
    wx.showModal({
      title: '提示',
      content: '确认订单已完成？',
      complete: (res) => {
        if (res.confirm) {
          api.customerCompleteRepairOrder(order_sn)
            .then(res => {
              wx.showToast({
                title: '操作成功',
                icon: 'success'
              })
              const status = that.data.repair_order.status
              that.orderDetail(order_sn);
              let pages = getCurrentPages();
              let beforePage = pages[pages.length -2];
              beforePage.setData({
                list: {}
              })
              beforePage.orderList(status)
            }).catch(err => console.log(err))
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})